/*
 * This file is part of LexiToolsMobile
 *  
 * Author: Jerzy Wawro
 * ver. 1.00
 * Copyright: Tenar (http://www.tenar.pl)
 *
 * Mobile LexiTools is an open source project; you can redistribute it 
 * and/or modify it under the terms of the license as published -  GPL with 
 * the following modification:  fragments of the code may be not copyleft 
 * (hidden), to protect contents of the dicionaries.
 * In this case the author of non-public fragments of the code is obliged 
 * to make available functionally equivalent code for unprotected dictionaries.
 * 
 * Mobile LexiTools is distributed WITHOUT ANY WARRANTY.
 * See the license for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with LexiToolsMobile (see: gpl.txt); if not - you can find it 
 * on web page http://www.fsf.org/, or write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com;

import javax.microedition.io.Connector;
//import javax.wireless.messaging.*;

import javax.microedition.lcdui.*;

import java.io.IOException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;


public class SMSSender implements Runnable {
    private String smsPort;
    private String message;
    private StringBuffer address;
    private Callback callback;
    
    
    public SMSSender() {
    }
    
    public void send(Callback callback, String smsPort, String phoneno, String message){
        this.smsPort = smsPort;
        this.message = message;
        this.callback=callback;
        address = new StringBuffer("sms://");
        address.append(phoneno);
        new Thread(this).start();
    }
    
    public void run() {
        boolean ok;
        String msg;
        
        ok = true;msg="";
        MessageConnection smsconn = null;
        try {
            smsconn = (MessageConnection)Connector.open(address.toString());
            while (smsconn==null) {
                Thread.sleep(10);
            }
            TextMessage txtmessage = (TextMessage)smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
            txtmessage.setPayloadText(message);
            txtmessage.setAddress(address.toString());
            smsconn.send(txtmessage);
//            smsconn.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Send caught: ");
            ok=false;
            msg=e.getMessage();
        }
       if (smsconn != null) {
            try {
                smsconn.close();
            } catch (IOException ioe) {
                System.out.println("Closing connection caught: ");
                ioe.printStackTrace();
                ok=false;
                msg=ioe.getMessage();
            }
        }
        if (ok) {
            callback.cb_completed(0, "ok");
        }
        else {
            callback.cb_failure(0, msg);
        }
      }
    
}

    